<script>
    Namespace.register("EBCMS.{$namespace}type");
    $(function(){
        EBCMS.{$namespace}type.refresh = function refresh(){
            EBCMS.CORE.api({
                queryParams:{
                    order:{
                        'sort':'desc',
                        'id':'asc',
                    },
                    model:'content/category',
                },
                tree:true,
                rootitem:true,
                treelevel:true,
                loadAfter:function(data){
                    EBCMS.CORE.compile({
                        data:data,
                        tpl:'{$namespace}type-table',
                        target:'#{$namespace}type_table',
                        compileAfter:function(p){
                            var $lists = $(p.target).find('a.list-group-item');
                            $.each($lists, function(index, val) {
                                var ids = [];
                                ids.push($(val).data('id'));
                                if ($(val).next().is('div')) {
                                    var sub = $(val).next().find('a.list-group-item');
                                    $.each(sub, function(n, v) {
                                        ids.push($(v).data('id'));
                                    });
                                }
                                if ($(val).next().is('div')) {
                                    if ($(val).next().css('display') == 'none') {
                                        $(val).prepend('<span class="iconfont icon-zhankai icon-shouqi pull-right"></span>');
                                    }else{
                                        $(val).prepend('<span class="iconfont icon-zhankai pull-right"></span>');
                                    }
                                    $('span',val).bind('click',function(event) {
                                        EBCMS.{$namespace}type.nochange = true;
                                        $(this).toggleClass('icon-shouqi');
                                        $(val).next().toggle(150);
                                        setTimeout(function(){
                                            EBCMS.{$namespace}type.nochange = false;
                                        }, 150);
                                    });
                                }
                                $(val).bind('click', function(event) {
                                    if (true != EBCMS.{$namespace}type.nochange) {
                                        $lists.removeClass('active');
                                        $(this).addClass('active');
                                        EBCMS.{$namespace}type.changecate(ids);
                                        
                                        $(this).find('span').click();
                                        $.each($(this).siblings('a'), function(index, v) {
                                            if (!$('span',v).hasClass('icon-shouqi')) {
                                                $('span',v).click();
                                            }
                                        });
                                    }
                                });
                            });
                        }
                    });
                }
            });
        };
        EBCMS.{$namespace}type.changecate = function changecate(category_ids){
            EBCMS.CORE.compile({
                tpl:'{$namespace}-box',
                target:'#{$namespace}_box',
                compileAfter:function(){
                    EBCMS.{$namespace}.category_id = category_ids[0];
                    EBCMS.{$namespace}.refresh({
                        page:1,
                        where:{
                            category_id:['in',category_ids],
                        }
                    });
                }
            });
        };
        /*合拢&展开*/
        EBCMS.{$namespace}type.closure = function(eml){
            if ($('#{$namespace}type_table').find('.icon-shouqi').length) {
                $('#{$namespace}type_table').find('.icon-shouqi').click();
                $(eml).html('堆叠');
            }else{
                /*合拢*/
                $('#{$namespace}type_table div.list-group-item span.icon-zhankai').click();
                $(eml).html('展开');
            }
        };
        EBCMS.CORE.compile({
            tpl:'{$namespace}type-box',
            target:'#{$namespace}type_box',
            compileAfter:function(){
                EBCMS.{$namespace}type.refresh();
            }
        });
    });
</script>

<script id="{$namespace}type-box" type="text/html">
    <div class="header">
        <div class="btn-group pull-right">
            <div class="btn-group">
              <button type="button" class="btn btn-primary" onclick="EBCMS.{$namespace}type.closure($(this));">展开</button>
            </div>
        </div>
        <div class="header-title" onclick="EBCMS.{$namespace}type.refresh();">栏目</div>
    </div>
    <div class="body" style="bottom:0px;">
        <div class="box" id="{$namespace}type_table"></div>
    </div>
</script>

<script id="{$namespace}type-table" type="text/html">
    <div class="list-group tree tree_first" style="border-top: 1px solid #ddd;border-right: 1px solid #ddd;border-bottom: 1px solid #ddd;">
        [[include '{$namespace}type-table-item']]
    </div>
</script>

<script id="{$namespace}type-table-item" type="text/html">
    [[each rows as v n]]
        <a class="list-group-item" data-id='[[v.id]]' href="###">[[v.title]][[if v.pid>=0 && v.status!=1]]&nbsp;<i class="iconfont icon-tishi2"></i>[[/if]][[if v.pid>=0 && v.ebcms_url!='']]&nbsp;<i class="iconfont icon-url"></i>[[/if]]</a>
        [[if v.rows]]
            <div class="list-group-item nopadding" [[if v.pid==0]]style="display:none;"[[/if]]>
                <div class="list-group tree nopadding">
                    [[include '{$namespace}type-table-item' v]]
                </div>
            </div>
        [[/if]]
    [[/each]]
</script>